import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard/analysis',
        name: 'Root',
        meta: {
            hidden: true
        }
    },
    {
        path: '/redirect',
        component: Layout,
        name: 'Redirect',
        children: [
            {
                path: '/redirect/:path(.*)',
                name: 'Redirect',
                component: () => import('@/views/Redirect/Redirect.vue'),
                meta: {}
            }
        ],
        meta: {
            hidden: true,
            noTagsView: true
        }
    },
    {
        path: '/login',
        component: () => import('@/views/Login/Login.vue'),
        name: 'Login',
        meta: {
            hidden: true,
            title: t('router.login'),
            noTagsView: true
        }
    },
    {
        path: '/404',
        component: () => import('@/views/Error/404.vue'),
        name: 'NoFind',
        meta: {
            hidden: true,
            title: '404',
            noTagsView: true
        }
    }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
    {
        path: '/dashboard',
        component: Layout,
        redirect: '/dashboard/analysis',
        name: 'Dashboard',
        meta: {
            title: '首页',
            icon: 'ant-design:dashboard-filled',
            alwaysShow: true
        },
        children: [
            {
                path: 'analysis',
                component: () => import('@/views/Frontpage/Analysis.vue'),
                name: 'Analysis',
                meta: {
                    title: t('router.analysis'),
                    noCache: true,
                    affix: false
                }
            },
            {
                path: 'register_log',
                component: () => import('@/views/Frontpage/Register_log.vue'),
                name: 'Register_log',
                meta: {
                    title: t('router.register_log'),
                    noCache: true,
                    affix: false
                }
            },
            {
                path: 'system_infor',
                component: () => import('@/views/Frontpage/System_infor.vue'),
                name: 'System_infor',
                meta: {
                    title: t('router.system_infor'),
                    noCache: true,
                    affix: false
                }
            },
            {
                path: 'account_setting',
                component: () => import('@/views/Frontpage/Account_setting.vue'),
                name: 'Account_setting',
                meta: {
                    title: t('router.account_setting'),
                    noCache: true,
                    affix: false
                }
            },
        ]
    },
    {
        path: '/tongji',
        component: Layout,
        redirect: '/tongji/kechengshoucang',
        name: 'Tongji',
        meta: {
            title: '统计',
            icon: 'carbon:skill-level-advanced',
            alwaysShow: true
        },
        children: [
            {
                path: 'kechengshoucang',
                component: () => import('@/views/Tongji/CourseShoucang.vue'),
                name: 'KechengShoucang',
                meta: {
                    title: '课程收藏统计',
                    noCache: true
                }
            },
            {
                path: 'kechengdianzan',
                component: () => import('@/views/Tongji/dianzan/CourseDianzan.vue'),
                name: 'KechengDianzan',
                meta: {
                    title: '课程点赞统计',
                    noCache: true
                }
            },
            {
                path: 'weiquanshoucang',
                component: () => import('@/views/Tongji/weiquanshoucang/WeiquanShouCang.vue'),
                name: 'weiquanshoucang',
                meta: {
                    title: '微圈收藏统计',
                    noCache: true
                }
            },
            {
                path: 'weiquandianzan',
                component: () => import('@/views/Tongji/weiquandianzan/WeiquanDianZan.vue'),
                name: 'weiquandianzan',
                meta: {
                    title: '微圈点赞统计',
                    noCache: true
                }
            },
            {
                path: 'coursegoumai',
                component: () => import('@/views/Tongji/coursegoumai/CourseGouMai.vue'),
                name: 'coursegoumai',
                meta: {
                    title: '课程购买统计',
                    noCache: true
                }
            },
            {
                path: 'sousuoguanjianzi',
                component: () => import('@/views/Tongji/sousuoguanjianzi/SousuoGuanJianZi.vue'),
                name: 'sousuoguanjianzi',
                meta: {
                    title: '搜索关键字统计',
                    noCache: true
                }
            },
            {
                path: 'kechengguankan',
                component: () => import('@/views/Tongji/kechengguankan/KechengGuanKan.vue'),
                name: 'kechengguankan',
                meta: {
                    title: '课程观看量统计',
                    noCache: true
                }
            },
            {
                path: 'weiquanliulan',
                component: () => import('@/views/Tongji/weiquanliulan/WeiquanYulan.vue'),
                name: 'weiquanliulan',
                meta: {
                    title: '微圈浏览量统计',
                    noCache: true
                }
            },
            {
                path: 'shopshoucang',
                component: () => import('@/views/Tongji/shopshoucang/ShopShoucang.vue'),
                name: 'shopshoucang',
                meta: {
                    title: '商品收藏统计',
                    noCache: true
                }
            },
            {
                path: 'shopdianzan',
                component: () => import('@/views/Tongji/shopdianzan/ShopDianzan.vue'),
                name: 'shopdianzan',
                meta: {
                    title: '商品点赞统计',
                    noCache: true
                }
            },
            {
                path: 'shopgoumai',
                component: () => import('@/views/Tongji/shopgoumai/ShopGoumai.vue'),
                name: 'shopgoumai',
                meta: {
                    title: '商品购买统计',
                    noCache: true
                }
            },
            {
                path: 'denglutongji',
                component: () => import('@/views/Tongji/denglutongjitu/DengluTu.vue'),
                name: 'denglutongji',
                meta: {
                    title: '登录统计',
                    noCache: true
                }
            },
            {
                path: 'fankuitongji',
                component: () => import('@/views/Tongji/fankuitongji/FankuiTongji.vue'),
                name: 'fankuitongji',
                meta: {
                    title: '反馈统计',
                    noCache: true
                }
            }
        ]
    },
    {
        path: '/quanxiantable',
        component: Layout,
        name: 'quanxiantable',
        meta: {
            title: '权限',
            icon: 'ri:lock-2-line',
            alwaysShow: true
        },
        children: [
            {
                path: 'quanxian',
                component: getParentLayout(),
                name: 'quanxian',
                meta: {
                    title: '权限管理',
                    alwaysShow: true
                },
                children: [
                    {
                        path: 'bumen',
                        component: () => import('@/views/QuanXian/quanxianguanli/bumenguanli/bumen/BumenGuanli.vue'),
                        name: 'bumen',
                        meta: {
                            title: '部门管理列表'
                        }
                    },
                    {
                        path: 'houtaiguanlirenyuan',
                        component: () => import('@/views/QuanXian/quanxianguanli/houtaiguanli/BmpList.vue'),
                        name: 'houtaiguanlirenyuan',
                        meta: {
                            title: '后台管理人员列表'
                        }
                    },
                    {
                        path: 'kefuguanli',
                        component: () => import('@/views/QuanXian/quanxianguanli/kefuguanli/Kefu.vue'),
                        name: 'kefuguanli',
                        meta: {
                            title: '客服管理'
                        }
                    },
                    {
                        path: 'banben',
                        component: () => import('@/views/QuanXian/quanxianguanli/houtaibanben/Banben.vue'),
                        name: 'banben',
                        meta: {
                            title: '后台版本管理'
                        }
                    },
                ]
            },
            {
                path: 'rizhiguanli',
                component: getParentLayout(),
                name: 'rizhiguanli',
                meta: {
                    title: '日志管理',
                    alwaysShow: true
                },
                children: [
                    {
                        path: 'caozuorizhi',
                        component: () => import('@/views/QuanXian/rizhiguanli/caozuorizhi/Caozuo.vue'),
                        name: 'caozuorizhi',
                        meta: {
                            title: '操作日志'
                        }
                    },
                    {
                        path: 'houtaidenglu',
                        component: () => import('@/views/QuanXian/rizhiguanli/houtaidenglu/HoutaiDenglu.vue'),
                        name: 'houtaidenglu',
                        meta: {
                            title: '后台管理员登录日志'
                        }
                    },
                    {
                        path: 'qiantaidenglu',
                        component: () => import('@/views/QuanXian/rizhiguanli/qiantaidenglu/QiantaiDenglu.vue'),
                        name: 'qiantaidenglu',
                        meta: {
                            title: '前台用户登录日志'
                        }
                    },
                    {
                        path: 'fenghaorizhi',
                        component: () => import('@/views/QuanXian/rizhiguanli/fenghaorizhi/Fenghao.vue'),
                        name: 'fenghaorizhi',
                        meta: {
                            title: '封号日志'
                        }
                    }
                ]
            },
            {
                path: 'fankui',
                component: getParentLayout(),
                redirect: '/components/editor-demo/editor',
                name: 'fankui',
                meta: {
                    title: '反馈管理',
                    alwaysShow: true
                },
                children: [
                    {
                        path: 'fankuiguanli',
                        component: () => import('@/views/QuanXian/fankuiguanli/fankuiliebiao/fankui/Fankui.vue'),
                        name: 'fankuiguanli',
                        meta: {
                            title: '反馈列表'
                        }
                    }
                ]
            }
        ]
    },
    //优惠卷
    {
        path: '/example',
        component: Layout,
        redirect: '/example/example-dialog',
        name: 'Example',
        meta: {
            title: t('router.coupon'),
            icon: 'gg:loadbar-doc',
            alwaysShow: true
        },
        children: [
            {
                path: 'example-dialog',
                component: () => import('@/views/Example/Dialog/ExampleDialog.vue'),
                name: 'ExampleDialog',
                meta: {
                    title: t('router.offerList')
                }
            },
            {
                path: 'example-page',
                component: () => import('@/views/Example/Page/ExamplePage.vue'),
                name: 'ExamplePage',
                meta: {
                    title: t('router.viewusercoupons')
                }
            },
            //添加
            {
                path: 'example-add',
                component: () => import('@/views/Example/Dialog/ExampleAdd.vue'),
                name: 'ExampleAdd',
                meta: {
                    title: t('router.exampleAdd'),
                    noTagsView: true,
                    noCache: true,
                    hidden: true,
                    canTo: true,
                    activeMenu: '/example/example-dialog'
                }
            },
            {
                path: 'example-edit3',
                component: () => import('@/views/Example/Page/ExampleEdit.vue'),
                name: 'ExampleEdit',
                meta: {
                    title: t('router.exampleEdit'),
                    noTagsView: true,
                    noCache: true,
                    hidden: true,
                    canTo: true,
                    activeMenu: '/example/example-page'
                }
            },
            {
                path: 'example-detail',
                component: () => import('@/views/Example/Page/ExampleDetail.vue'),
                name: 'ExampleDetail',
                meta: {
                    title: t('router.exampleDetail'),
                    noTagsView: true,
                    noCache: true,
                    hidden: true,
                    canTo: true,
                    activeMenu: '/example/example-page'
                }
            }
        ]
    },
    //广告
    {
        path: '/advertisement',
        component: Layout,
        redirect: '/advertisement/example-dialog',
        name: 'Advertisement',
        meta: {
            title: t('router.advertisement'),
            icon: 'ep:management',
            alwaysShow: true
        },
        children: [
            {
                path: 'carousel-ChartList',
                component: () => import('@/views/Advertisement/Dialog/ExampleDialog.vue'),
                name: 'CarouselChartList',
                meta: {
                    title: t('router.carouselChartList')
                }
            },
            {
                path: 'add-arotationchart',
                component: () => import('@/views/Advertisement/Page/ExamplePage.vue'),
                name: 'Addarotationchart',

                meta: {
                    title: t('router.addarotationchart')
                }
            },
            {
                path: 'example-add',
                component: () => import('@/views/Advertisement/Page/ExamplePage.vue'),
                name: 'ExampleAdd',
                meta: {
                    title: t('router.exampleAdd'),
                    noTagsView: true,
                    noCache: true,
                    hidden: true,
                    canTo: true,
                    activeMenu: '/example/example-page1'
                }
            },
            {
                path: 'example-edit',
                component: () => import('@/views/Advertisement/AddCarousel.vue'),
                name: 'ExampleEdit',
                meta: {
                    title: t('router.exampleEdit'),
                    noTagsView: true,
                    noCache: true,
                    hidden: true,
                    canTo: true,
                    activeMenu: '/example/example-page'
                }
            },
            {
                path: 'example-detail123',
                component: () => import('@/views/Advertisement/AddCarousel.vue'),
                name: 'ExampleDetail123',
                meta: {
                    title: t('router.exampleDetail'),
                    noTagsView: true,
                    noCache: true,
                    hidden: true,
                    canTo: true,
                    activeMenu: '/example/example-page'
                }
            }
        ]
    },
    {
        path: '/commodity',
        component: Layout,
        redirect: '/commodity/commodityList/m2',
        name: 'Commodity',
        meta: {
            title: '商品',
            alwaysShow: true
        },
        children: [
            {
                path: 'commodityList',
                component: () => import('@/views/Commodity/CommodityList.vue'),
                name: 'CommodityList',
                meta: {
                    title: '商品列表'
                }
            },
            {
                path: 'PersonalProducts',
                component: () => import('@/views/Commodity/PersonalProducts.vue'),
                name: 'PersonalProducts',
                meta: {
                    title: '个人商品列表'
                }
            },
            {
                path: 'reviewTheProductList',
                component: () => import('@/views/Commodity/ReviewTheProductList.vue'),
                name: 'ReviewTheProductList',
                meta: {
                    title: '审核商品列表'
                }
            },
            {
                path: 'logManagement',
                component: () => import('@/views/Commodity/OperationalLogs.vue'),
                name: 'LogManagement',
                meta: {
                    title: '日志管理'
                },
                children:[
                    {
                        path: 'operationalLogs',
                        component: () => import('@/views/Commodity/OperationalLogs.vue'),
                        name: 'OperationalLogs',
                        meta: {
                            title: '操作日志'
                        }
                    },
                    {
                        path: 'productAuditLog',
                        component: () => import('@/views/Commodity/ProductAuditLog.vue'),
                        name: 'ProductAuditLog',
                        meta: {
                            title: '商品审核日志'
                        }
                    },
                ]
            }
        ]
    },
    {
        path: '/examplee',
        component: Layout,
        redirect: '/examplee/examplee-dialog',
        name: 'Example',
        meta: {
            title: '用户评论',
            icon: 'ep:management',
            alwaysShow: true
        },
        children: [
            {
                path: 'ExampleDialogComment',
                component: () => import('@/views/Example/Dialog/ExampleDialogComment.vue'),
                name: 'ExampleDialogComment',
                meta: {
                    title: t('router.ExampleDialogComment')
                }
            },
            {
                path: 'ExampleDialogMicroreviewTab',
                component: () => import('@/views/Example/Dialog/ExampleDialogMicroreviewTab.vue'),
                name: 'ExampleDialogMicroreviewTab',
                meta: {
                    title: t('router.ExampleDialogMicroreviewTab')
                }
            },
        ]
    },
    // {
    //   path: '/authorization',
    //   component: Layout,
    //   redirect: '/authorization/user',
    //   name: 'Authorization',
    //   meta: {
    //     title: t('router.authorization'),
    //     icon: 'eos-icons:role-binding',
    //     alwaysShow: true
    //   },
    //   children: [
    //     {
    //       path: 'user',
    //       component: () => import('@/views/Authorization/User.vue'),
    //       name: 'User',
    //       meta: {
    //         title: t('router.user')
    //       }
    //     },
    //     {
    //       path: 'role',
    //       component: () => import('@/views/Authorization/Role.vue'),
    //       name: 'Role',
    //       meta: {
    //         title: t('router.role')
    //       }
    //     }
    //   ]
    // }
]

const router = createRouter({
    history: createWebHashHistory(),
    strict: true,
    routes: constantRouterMap as RouteRecordRaw[],
    scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
    const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
    router.getRoutes().forEach((route) => {
        const { name } = route
        if (name && !resetWhiteNameList.includes(name as string)) {
            router.hasRoute(name) && router.removeRoute(name)
        }
    })
}

export const setupRouter = (app: App<Element>) => {
    app.use(router)
}

export default router
